package com.example.design.shejimoshi.action.strategy.holder;

import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @date 2023/4/21 15:22
 * @Author liuzhihui
 * @Version 1.0
 */
public class StrategyHolder<T extends StrategyType> {
    List<T> types;

    Map<Integer, T> typeMap;

    public StrategyHolder(@Autowired List<T> types) {
        this.types = types;
    }

    @PostConstruct
    protected void a(){
        typeMap = types.stream().collect(Collectors.toMap(StrategyType::getType, a->a));
    }

    public StrategyType getType(Integer type) {
        return typeMap.get(type);
    }
}
